// tomatoClock.js
Page({
  data: {
    timeLeft: 25 * 60, // 默认25分钟
    isRunning: false,
    timer: null,
    currentMode: 'work' // 'work' or 'break'
  },
  startTimer: function () {
    if (!this.data.isRunning) {
      this.setData({ isRunning: true })
      this.data.timer = setInterval(() => {
        if (this.data.timeLeft > 0) {
          this.setData({ timeLeft: this.data.timeLeft - 1 })
        } else {
          this.switchMode()
        }
      }, 1000)
    }
  },
  pauseTimer: function () {
    if (this.data.isRunning) {
      clearInterval(this.data.timer)
      this.setData({ isRunning: false })
    }
  },
  resetTimer: function () {
    clearInterval(this.data.timer)
    const duration = this.data.currentMode === 'work' ? 25 : 5
    this.setData({
      timeLeft: duration * 60,
      isRunning: false
    })
  },
  switchMode: function () {
    clearInterval(this.data.timer)
    const newMode = this.data.currentMode === 'work' ? 'break' : 'work'
    const duration = newMode === 'work' ? 25 : 5
    this.setData({
      currentMode: newMode,
      timeLeft: duration * 60,
      isRunning: false
    })
  },
  formatTime: function (seconds) {
    const minutes = Math.floor(seconds / 60)
    const remainingSeconds = seconds % 60
    return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`
  }
})
